package com.dongyun.loraweb.controller;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.dongyun.loraweb.dao.SystemAccountRepository;
import com.dongyun.loraweb.entity.SystemAccount;
import com.dongyun.loraweb.json.Result;

import io.swagger.annotations.ApiOperation;

@RestController
public class LoginController extends AbstractController {
	Logger LOGGER = LoggerFactory.getLogger(LoginController.class);
	@Autowired
	private SystemAccountRepository systemAccountRepository;
	@Autowired
	private HttpServletRequest req;
	@ApiOperation("用户登录")
	@RequestMapping("/loginUser")
	public Result login(@RequestParam(defaultValue="") String userName,@RequestParam(defaultValue="") String password){
		LOGGER.info("用户：{} 登录",userName);
		SystemAccount systemAccount = new SystemAccount();
		systemAccount.setPassword(password);
		systemAccount.setUserName(userName);
		systemAccount = systemAccountRepository.findOne(Example.of(systemAccount));
		if(systemAccount==null){
			return createResult("用户名或密码错误！");
		}
		req.getSession().setAttribute(SystemAccount.class.getSimpleName(), systemAccount);
		return createResult(true,"登录成功");
	}
}
